
local w0,h0 = 322,200
local ui = class.panel:builder 
{
    x = 1920 - w0,
    y = 320,
    w = w0,
    h = h0,
    fdf = 'tooltip_backdrop1',
}
--ui:add_text('排行榜',0,5,w0,25,12,5)

local panels = {}
local size = 5
local w = 291
local h = 40
local color = {
    '|cffff0000%s|r',
    '|cff0080ff%s|r',
    '|cFF1CE6B9%s|r',
    '|cffa62dff%s|r',
}
for a=1,4 do
    local panel = ui:add_panel('',15,(a-1)*(h+5)+10,w-40,h)
    panel.icon = panel:add_panel([[ReplaceableTextures\CommandButtons\BTNSkeletonArcher.blp]],panel.w,0,h,h)
    panel.load1 = panel:add_panel([[ReplaceableTextures\TeamColor\TeamColor05.blp]],0,5,panel.w*0.5,panel.h-10)
    panel.load2 = panel:add_panel([[core\hpbar\蓝条.tga]],0,5,panel.w,panel.h-10)
    panel.dps = panel:add_text('999999',10,0,panel.w,h,12,4,1)
    panel.name = panel:add_text(color[a]:format(ac.player[a]:get_name()),0,0,panel.w-5,nil,12,6,1)
    panel.load_w = panel.w - 10

    --panel.dps:set_color(0xcffffff00)
    panels[a] = panel
end

function ui:on_button_clicked(button)
    if button then
        self.mode = button.name
        if self.select_map then
            self.select_map.text:set_color(0xffffffff)
        end
        self.select_map = button
        self.select_map.text:set_color(0xff00ff00)
    end
end





local key1 = '物理伤害统计'
local key2 = '魔法伤害统计'
ui.list1 = {}
ui.list2 = {}
function ui:up_date()
    local max = 0
    local time = math.min(ac.clock()/1000,60)
    -- table.sort(self.list,function(a,b)
    --     return (a['总伤害统计'] or 0)>(b['总伤害统计'] or 0)
    -- end)
    local list = self.list1
    if self.mode=='每秒' then
        list = self.list2
    end
    for a,panel in ipairs(panels) do
        local info = list[a]
        if info then
            local player = info.player
            local hero = player.hero
            local dps2 = info[key1] or 0
            local dps3 = info[key2] or 0
            local dps1 = math.max(dps2 + dps3,1)
            local rate1 = dps2/dps1
            local rate2 = dps3/dps1
            panel.name:set_text(player:get_color_name())
            panel.load1:set_width(rate1*panel.load_w+0.01)
            panel.load2:set_width(rate2*panel.load_w+0.01)
            panel.load1:set_position(rate2*panel.load_w+5,panel.load1.y)

            panel.dps:set_text(format_number1(dps1))
            if hero then
                panel.icon:set_normal_image(hero:get_slk("Art", ""))
            end
            panel:show()
        else
            panel:hide()
        end
    end
end


ui.mode1 = ui:add_namebutton('累计',100,-40,80,40,12,5,'tooltip_backdrop1')
ui.mode2 = ui:add_namebutton('每秒',ui.mode1.x + ui.mode1.w,-40,80,40,12,5,'tooltip_backdrop1')

ui:on_button_clicked(ui.mode2)

local esc = class.button.create('',1920-40,ui.y-40,40,40)
esc:add_frame('tooltip_backdrop1')
esc.text = esc:add_text('<<',0,0,nil,nil,12,5)
esc:set_highlight([[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]])

function esc:on_button_clicked()
    if ui:is_show() then
        esc.text:set_text('<<')
        ui:hide()
    else
        esc.text:set_text('>>')
        ui:show()
    end
end
esc:on_button_clicked()
esc:on_button_clicked()

ac.loop(30,function()
    ui:up_date()
end)

ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    local list1 = {
        player = player,
        ['物理伤害统计'] = 0,
        ['魔法伤害统计'] = 0,
        ['总伤害统计'] = 0,
    }
    local list2 = {
        player = player,
        ['物理伤害统计'] = 0,
        ['魔法伤害统计'] = 0,
        ['总伤害统计'] = 0,
    }
    table.insert(ui.list1,list1)
    table.insert(ui.list2,list2)
    hero:event '单位-造成伤害结束'(function(_,_,_,data)
        local damage = data.real_damage
        local key = '物理伤害统计'
        if data.damage_type=='魔法' then
            key = '魔法伤害统计'
        end
        list1[key] = list1[key] + damage
        list1['总伤害统计'] = list1['总伤害统计'] + damage
        

        list2[key] = list2[key] + damage / 10
        list2['总伤害统计'] = list2['总伤害统计'] + damage / 10
        ac.wait(10*1000,function()
            list2[key] = list2[key] - damage / 10
            list2['总伤害统计'] = list2['总伤害统计'] - damage / 10
        end)
    end)
end)